﻿using Quartz;
using Schedule.RequestModel;

namespace Schedule.Method
{
    [DisallowConcurrentExecution]//防止任务并行
    public class MessageLoadJob : IJob
    {
        private readonly ILogger<MessageLoadJob> logger;

        public MessageLoadJob(ILogger<MessageLoadJob> logger)
        {
            this.logger = logger;
        }

        public async Task Execute(IJobExecutionContext context)
        {
            var request = (RecordRequest)context.JobDetail.JobDataMap.Get("request");
            using (var client = new HttpClient())
            {
                try
                {
                    logger.LogInformation("执行前" + request + "结果:");
                    string s = await client.GetStringAsync(request.Uri);
                    if (!string.IsNullOrEmpty(s))
                        logger.LogInformation("执行后" + request + "结果:" + s);
                }
                catch (Exception ex)
                {
                    logger.LogError("执行" + request + "时出错:" + ex.Message);
                }
            }

        }
    }
}
